package com.liner.string;

import java.util.Scanner;

/**
 * 返回当前字符串指定位置上的字符,根据位置找字符
 */
public class CharAtDemo {
    static  Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        String str = "hello123123";

        System.out.println(str.charAt(0));
        //StringIndexOutOfBoundsException
        //System.out.println(str.charAt(12));


        System.out.print("输入想要生成几位数验证码:");
        int num = scanner.nextInt();
        String code = verCode(num);
        System.out.println(code);

    }

    /**
     * 生成N位验证码
     */
    public static String verCode(int len) {
        String code = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrlstuvwxyz";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int index = (int) (Math.random() * code.length());
            sb.append(code.charAt(index));
        }
        return sb.toString();
    }



}
